package calorie.com.calorie.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.Md5Utils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.LoginContact;
import calorie.com.calorie.response.ResultDTO;
import calorie.com.calorie.response.User;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends MvpActivity<LoginContact.LoginPresent> implements LoginContact.ILoginView {
    @BindView(R.id.et_login_phone)
    EditText et_login_phone;
    @BindView(R.id.et_login_password)
    EditText et_login_password;
    @BindView(R.id.bt_login_submit)
    AppCompatButton bt_login_submit;
    @BindView(R.id.auto_login_text)
    TextView auto_login_text;
    @BindView(R.id.auto_login_checkBox)
    CheckBox auto_login_checkBox;

    @Override
    protected LoginContact.LoginPresent createPresent() {
        return new LoginContact.LoginPresent();
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

/*    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        et_login_phone.setText("");
        et_login_password.setText("");
    }*/

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void getLoginData(Response<ResultDTO> response) {
        dismissLoadingDialog();
        ResultDTO loginResponse = response.body();
        String code = loginResponse.getCode();
        switch (code) {
            case "200":
                Gson gs = new Gson();
                User userBean = gs.fromJson(gs.toJson(loginResponse.getData()), User.class);
                StaticValue.user = userBean;
                if (!ObjectUtils.isEmpty(userBean)) {
                    SPUtils.putInt(Constant.USER_ID, userBean.getId(), context);
                    SPUtils.putString(Constant.PHONE, userBean.getMobilePhone(), context);
                    SPUtils.putString(Constant.INVITE_CODE, userBean.getInvitationCode(), context);
                    SPUtils.putString(Constant.LOGIN_PWD, userBean.getLoginPassword(), context);
                    SPUtils.putString(Constant.NICK_NAME, userBean.getNickName(), context);
                    SPUtils.putString(Constant.ACCOUNT, userBean.getAccount(), context);
                    SPUtils.putString(Constant.RECEIVE_ADDRESS, userBean.getReceivingAddress(), context);
                    SPUtils.putString(Constant.RECEIVE_NAME,userBean.getReceivingName(),context);
                    SPUtils.putString(Constant.RECEIVE_PHONE,userBean.getReceivingPhone(),context);
                    SPUtils.putString(Constant.LOGIN_TIME, userBean.getLoginTime(), context);
                    SPUtils.putInt(Constant.STATUS, userBean.getStatus(), context);

                    if (auto_login_checkBox.isChecked()) {
                        SPUtils.putBoolean(Constant.AUTOLOGIN, true, context);
                    } else {
                        SPUtils.putBoolean(Constant.AUTOLOGIN, false, context);
                    }

                    if (userBean.getStatus() == -1) {//status等于-1，就代表第一次登陆 0代表未激活 1代表已经激活
                        startActivity(new Intent(context, FirstLoginModifyPwdActivity.class));
                    } else if (userBean.getMobilePhone() == null || userBean.getMobilePhone().equals("")) {
                        ToastUtils.showLongToast(context, "账号异常，请重新设置登录信息");
                        startActivity(new Intent(context, FirstLoginModifyPwdActivity.class));
                        this.finish();
                    } else {
                        Toast.makeText(context, "登录成功", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.putExtra(MainActivity.START_FLAG, "login");
                        startActivity(intent);
                        finish();
                    }
                }

                break;
            case "201":
                ToastUtils.showLongToast(context, "账号与密码不匹配");
                break;
            case "202":
                ToastUtils.showLongToast(context, "账号已经被封禁，请联系官方客服");
                break;
        }

    }

    @Override
    public void getDataFailed() {
        ToastUtils.showLongToast(context, "网络请求异常");
        dismissLoadingDialog();
    }

    @OnClick({R.id.bt_login_submit, R.id.tv_forgetpassowrd, R.id.auto_login_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_login_submit:
                String account = et_login_phone.getText().toString();
                String logPwd = et_login_password.getText().toString();
                if (ObjectUtils.isEmpty(account)) {
                    ToastUtils.showLongToast(context, "用户名不能为空");
                    return;
                }
                if (ObjectUtils.isEmpty(logPwd)) {
                    ToastUtils.showLongToast(context, "密码不能为空");
                    return;
                }
                showLoadingDialog();
                mPresent.login(context, account, Md5Utils.stringToMD5(logPwd));
                break;
            case R.id.tv_forgetpassowrd:
                startActivity(new Intent(context, ForgetLoginPasswordActivity.class));
                break;
            case R.id.auto_login_text:
                if (auto_login_checkBox.isChecked()) {
                    auto_login_checkBox.setChecked(false);
                } else {
                    auto_login_checkBox.setChecked(true);
                }

                break;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            ToastUtils.showLongToast(context, "程序退出");
            finish();
            System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

